<?php

namespace app\common\lib;

class Show
{
    /**
     * 返回数据封装
     * @param  array  $codeReturn
     * @param  array  $data
     * @param  int  $httpStatus
     * @return \think\response\Json
     */
    protected static function codeReturn(array $codeReturn = [], $data = null, $info = '', $httpStatus = 200)
    {
        if (empty($codeReturn)) {
            $codeReturn = config('status.success');
        }
        list($status, $message) = $codeReturn;
        $result = [
            "status" => $status,
            "message" => $info ?: $message,
            "result" => $data
        ];
        return json($result, $httpStatus);
    }

    /**
     * 成功返回
     * @param  array  $data
     * @param  int  $httpStatus
     * @return \think\response\Json
     */
    public static function success($data = [],$info = '',$httpStatus = 200)
    {
        return self::codeReturn(config('status.success'), $data, $info, $httpStatus);
    }

    /**
     * 失败返回
     * @param  array  $codeReturn
     * @param  string  $info
     * @param  int  $httpStatus
     * @return \think\response\Json
     */
    public static function fail(array $codeReturn = [], $info = '', $httpStatus = 500)
    {
        return self::codeReturn($codeReturn, null, $info, $httpStatus);
    }

    /**
     * @param $isSuccess
     * @param  array  $codeReturn
     * @param  array  $data
     * @param  string  $info
     * @param  int  $httpStatus
     * @return \think\response\Json
     */
    public static function failOrSuccess($isSuccess, $codeReturn = [], $data = null, $info = '', $httpStatus = 500)
    {
        if ($isSuccess) {
            return self::success($data);
        }
        return self::fail($codeReturn, $info, $httpStatus);
    }
}